﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PagedList;
using System.Data.Objects;

namespace DLL
{
    public class BillRepo
    {
        public void Add(Bill bill)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.Bill.Add(bill);
                dbContext.SaveChanges();
            }
        }

        public void Update(Bill bill)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.Entry(bill).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public Bill GetById(int billId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.Bill.Find(billId);
            }
        }

        public Bill GetPrintedBill(int billId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.Bill.Where(x => x.Id == billId && x.IsPrinted == true).FirstOrDefault();
            }
        }

        public List<Bill> GetActiveBills()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                List<pGetActiveBill_Result> activeBills = dbContext.pGetActiveBill().ToList();
                List<Bill> result = new List<Bill>();
                foreach (pGetActiveBill_Result b in activeBills)
                {
                    Bill b2 = new Bill
                    {
                        CreatedBy = b.CreatedBy,
                        CustomerAddress = b.CustomerAddress,
                        CustomerName = b.CustomerName,
                        CustomerPhone = b.CustomerPhone,                        
                        DayIndex = b.DayIndex,
                        Id = b.Id,
                        IsPrinted = b.IsPrinted,
                        IsReadyPaid = b.IsReadyPaid,
                        SaleTime = b.SaleTime,
                        TotalPrice = b.TotalPrice.HasValue ? b.TotalPrice.Value :  0                      
                    };
                    result.Add(b2);
                }
                return result;
            }
        }

        public pGetNextBillIdAndIndex_Result GetNextIndexInDay()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.pGetNextBillIdAndIndex().FirstOrDefault();
            }
        }

        public bool Delete(int billId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                bool result = false;
                Bill bill = GetById(billId);
                if (bill.IsPrinted == false)
                {
                    int countBillDetail = dbContext.BillDetail.Where(x => x.BillId == billId).Count();
                    if (countBillDetail == 0)
                    {
                        dbContext.Bill.Attach(bill);
                        dbContext.Bill.Remove(bill);
                        dbContext.SaveChanges();
                        result = true;
                    }
                }
                return result;
            }
        }

        public bool DeleteBillAndDetail(int billId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                bool result = true;
                try
                {
                    dbContext.pDeleteBillAndDetail(billId);
                }
                catch
                {
                    result = false;
                    throw;
                }
                return result;
            }
        }

        public List<pSearchBill_Result> SearchBill(DateTime? fromDate, DateTime? toDate,
            int? billId, int? billIndex, bool? isPrinted, string customerName, int? customerPhone, string productName, out int totalPrice)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var result = dbContext.pSearchBill(fromDate, toDate, billId, billIndex, isPrinted, customerName, customerPhone, productName).ToList();
                totalPrice = 0;
                foreach (var bill in result)
                {
                    totalPrice += bill.TotalPrice.Value;
                }
                return result;
            }
        }

        public bool Sale(int billId)
        {
            bool result = false;
            try
            {
                using (RetailManagementEntities dbContext = new RetailManagementEntities())
                {
                    ObjectParameter vbitSuccess = new ObjectParameter("vbitSuccess", typeof(bool));
                    dbContext.pUpdateRemainAmountAfterSale(billId, vbitSuccess);
                    result = Convert.ToBoolean(vbitSuccess.Value);
                }
            }
            catch 
            {
                result = false;
                throw;
            }
            return result;
        }


        public List<pSellStatistics_Result> SellStatistics(
            DateTime fromDate, DateTime toDate, int? productTypeId, int? producerId, string productName,
            out int totalSale, out int totalDiscount, out int totalBenefit, out int totalInput)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var result = dbContext.pSellStatistics(fromDate, toDate, productTypeId, producerId, productName).ToList();
                totalSale = 0;
                totalDiscount = 0;
                totalBenefit = 0;
                totalInput = 0;
                foreach (pSellStatistics_Result sell in result)
                {
                    totalSale += sell.TotalSalePrice.Value;
                    totalDiscount += sell.TotalDiscount.Value;
                    totalBenefit += sell.TotalBenefit.Value;
                    totalInput += sell.TotalInput.Value;
                }
                return result;
            }
        }
    }
}
